# -*- coding: utf-8 -*-
import scrapy
import re
from scrapy.selector import Selector


class StocksSpider(scrapy.Spider):
    name = 'stock'
    start_urls = ['https://hq.gucheng.com/gpdmylb.html']

    def parse(self, response):
        for href in response.css('a::attr(href)').extract():
            try:
                stock = re.search(r'S[HZ]\d{6}/', href)
                url = 'https://hq.gucheng.com/' + stock.group()
                yield scrapy.Request(url, callback=self.parse_stock)
            except:
                continue

    def parse_stock(self, response):
        infoDict = dict()
        stockInfo = response.css('.stock_top').extract()[0]
        stockprice = response.css('.s_price').extract()[0]
        stockname = response.css('.stock_title').extract()[0]
        stockname = Selector(text=stockname)
        stockprice = Selector(text=stockprice)
        stockInfo = Selector(text=stockInfo)
        infoDict['名字'] = re.search(r'>(.*?)</h1>', stockname.css('h1').extract()[0]).group(1)
        infoDict['编号'] = re.search(r'>(.*?)</h2>', stockname.css('h2').extract()[0]).group(1)
        infoDict['状态'] = re.search(r'>(.*?)</em>', stockname.css('em').extract()[0]).group(1)
        infoDict['时间'] = re.search(r'>(.*?)</time>', stockname.css('time').extract()[0]).group(1)
        price = stockprice.css('em').extract()
        infoDict['股价'] = re.search(r'>(.*?)</em>', price[0]).group(1)
        infoDict['涨跌额'] = re.search(r'>(.*?)</em>', price[1]).group(1)
        infoDict['涨跌幅'] = re.search(r'>(.*?)</em>', price[2]).group(1)
        keylist = stockInfo.css('dt').extract()
        valuelist = stockInfo.css('dd').extract()
        for i in range(len(keylist)):
            key = re.search(r'>(.*?)<', keylist[i], flags=re.S).group(1)
            key = str(key)
            key = key.replace('\n', '')
            try:
                val = re.search(r'>(.*?)<', valuelist[i], flags=re.S).group(1)
                val = str(val)
                val = val.replace('\n', '')
            except:
                val = '--'
            infoDict[key] = val
        yield infoDict
